<?php

class IndexController extends Site_Controller_Action
{

    public function init()
    {
        
    } 

    public function indexAction()
    {   
        $sessao = $this->_segmento;
        $this->view->dir_pro = 'public/upload/produto/';
        $this->view->dir_emp = 'public/upload/empresa/';
        $where = '';
        
        $cat = (int) $this->_getParam('cat',0);
        if($cat > 0) $where = ' CAT_ID = '.$cat.' AND ';
        
        $pesquisar = $this->_getParam('pesquisar','');
        if((int)$pesquisar > 0) $where = ' PRO_ID = '.$pesquisar.' AND ';
        else if($pesquisar > '') $where = ' PRO_NOM like "'.$pesquisar.'%" AND ';
        
        $cat = $this->db->fetchAll('SELECT SQL_CACHE CAT_ID,CAT_NOM FROM CATEGORIA WHERE CAT_STA = 1 and CTS_ID = '.$sessao->CTS_ID.' ORDER BY CAT_NOM ASC');
        $this->view->cat = $cat;
        
        
        $this->view->escolar = 0;
        //if($sessao->CTS_ID == 2 && Zend_Auth::getInstance()->getStorage()->read()->USU_ID < 1){
            //$where = '';
       if($sessao->CTS_ID == 2 && $where == '' && Zend_Auth::getInstance()->getStorage()->read()->USU_ID < 1){     
            $this->view->escolar = 1;
            $result = $this->db->fetchAll('SELECT SQL_CACHE * FROM EMPRESA WHERE EMP_STA = 1 and CTS_ID = '.$sessao->CTS_ID.' ORDER BY EMP_NOM ASC');
            $this->view->emp = $result;            
        }else{
            $query = 'SELECT SQL_CACHE * FROM PRODUTO WHERE '.$where.' PRO_STA = 1 and CTS_ID = '.$sessao->CTS_ID.' ORDER BY PRO_QTDACE DESC, PRO_NOM ASC';
            $result = $this->db->fetchAll($query);
            $this->view->prod = $this->paginacao($result);
        }
    }

}

